
import React from 'react'
import PropTypes from 'prop-types'
import { Modal } from 'antd'
import moment from 'moment'
import classnames from 'classnames'
import style from './DocModal.module.less'

// 获取当前最新值
function getCurrentValue (formRef, key) {
  if (!formRef || !formRef.current) {
    return false
  }
  const c = formRef.current
  const m = c.getFieldsValue([key])
  const v = m[key]
  if (v instanceof Array) {
    if (v.length === 0) {
      return false
    }
    return v.join()
  }
  return v || false
}

// 获取参数列表对象
function getPMap (params = []) {
  const m = {}
  params.forEach(p => {
    m[p.doc_parm_key] = p
  })
  return m
}

// 转化数据格式
function transferData (o, isDate) {
  let v
  if (moment.isMoment(o)) {
    v = o.format('YYYY-MM-DD')
  } else if (o instanceof Array) {
    v = o.join()
  } else if (isDate && Number(o)) {
    v = moment(o * 1000).format('YYYY-MM-DD')
  } else {
    v = o
  }
  return v
}

// 获取表单数据
function getFormData (m, paramFormKeyMap) {
  const keys = Object.keys(m)
  const result = {}
  keys.forEach(k => {
    result[k] = transferData(m[k], paramFormKeyMap[k] && paramFormKeyMap[k].isDate)
  })
  return result
}

// 根据paramFormKeyMap获取formParamKeyMap
function getFormParamKeyMap (paramFormKeyMap) {
  const keys = Object.keys(paramFormKeyMap)
  const result = {}
  keys.forEach(k => {
    const o = paramFormKeyMap[k]
    result[o.key] = {
      key: k,
      isDate: o.isDate
    }
  })
  return result
}

// 获取格式化的文本数组
function renderText (text = '', pMap = {}, varKey, paramFormKeyMap, formData) {
  const pArr = text.split('/n')
  return pArr.map((p, i) => {
    const vars = p.split(/(\$\{[^${}]*\})/m)
    const result = vars.map((v, j) => {
      const param = pMap[v]
      if (param) {
        const isCurrent = param.var_key === varKey
        const formKey = paramFormKeyMap[param.var_key] ? paramFormKeyMap[param.var_key].key : undefined
        const formParamData = formKey ? formData[formKey] : undefined
        const pValue = param.var_value
        const names = classnames(style.param, {
          [style.current]: isCurrent
        })
        return <span key={`${i}-${j}`} className={names}>{formParamData || pValue}</span>
      }
      return <span key={`${i}-${j}`} className={style.param}>{v}</span>
    })
    return <p className={style.p} key={i}>{result}</p>
  })
}

// 渲染文本列表
function renderDocList (list = [], varKey, paramFormKeyMap, formRef) {
  let m
  if (formRef && formRef.current) {
    m = formRef.current.getFieldsValue()
    m = getFormData(m, getFormParamKeyMap(paramFormKeyMap))
  }
  return list.map((li, i) => {
    return (
      <div key={i}>
        <h4>{i + 1}、{li.title}</h4>
        <div>{renderText(li.text_temp, getPMap(li.params), varKey, paramFormKeyMap, m)}</div>
      </div>
    )
  })
}

function DocModal ({ visible, onHide, formRef, varKey, paramFormKeyMap, textTemp }) {
  getCurrentValue(formRef, varKey)
  return (
    <Modal
      title='文书填充预览'
      width='800px'
      open={visible}
      footer={null}
      onCancel={onHide}
      destroyOnClose
      maskClosable
    >
      <div>
        {visible ? renderDocList(textTemp, varKey, paramFormKeyMap, formRef) : null}
      </div>
    </Modal>
  )
}

DocModal.proptypes = {
  title: PropTypes.string,
  visible: PropTypes.bool,
  onHide: PropTypes.func,
  textTemp: PropTypes.array,
  varKey: PropTypes.string,
  paramFormKeyMap: PropTypes.object,
  formRef: PropTypes.object
}

DocModal.defaultProps = {
  title: '',
  visible: false,
  onHide: () => {},
  textTemp: [],
  varKey: '',
  paramFormKeyMap: {},
  formRef: null
}

export default DocModal
